package com.imooc.miaosha.service;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.imooc.miaosha.dao.MiaoshaUserDao;
import com.imooc.miaosha.domain.MiaoshaUser;
import com.imooc.miaosha.exception.GlobalException;
import com.imooc.miaosha.redis.MiaoshaUserKey;
//import com.imooc.miaosha.exception.GlobalException;
//import com.imooc.miaosha.redis.MiaoshaUserKey;
import com.imooc.miaosha.redis.RedisService;
import com.imooc.miaosha.result.CodeMsg;
import com.imooc.miaosha.util.MD5Util;
import com.imooc.miaosha.util.UUIDUtil;
//import com.imooc.miaosha.util.UUIDUtil;
import com.imooc.miaosha.vo.LoginVo;

@Service
public class MiaoshaUserService {

	public static final String COOKI_NAME_TOKEN = "token";

	@Autowired
	MiaoshaUserDao miaoshaUserDao;

	@Autowired
	RedisService redisService;

	public MiaoshaUser getById(long id) {
		return miaoshaUserDao.getById(id);
	}
	
	public String login(HttpServletResponse response,LoginVo loginVo) {
		if (loginVo == null) {
			throw new GlobalException(CodeMsg.SERVER_ERROR);
		}
		String formPass = loginVo.getPassword();
		String mobile = loginVo.getMobile();
		MiaoshaUser user = getById(Long.parseLong(mobile));
		if (user == null) {
			throw new GlobalException(CodeMsg.MOBILE_NOT_EXIST);
		}
		String dbPass = user.getPassword();
		String saltDB = user.getSalt();
		String formPassToDBPass = MD5Util.formPassToDBPass(formPass, saltDB);
		if (!formPassToDBPass.equals(dbPass)) {
			throw new GlobalException(CodeMsg.PASSWORD_ERROR);
		}
		
		String token = UUIDUtil.uuid();
		addCookie(response,user,token);
		return token;
	}

	public MiaoshaUser getByToken(String token,HttpServletResponse response) {
		if (StringUtils.isEmpty(token)) {
			return null;
		}
		MiaoshaUser user = redisService.get(MiaoshaUserKey.token, token, MiaoshaUser.class);
		//延长有效期
		if (user != null) {
			addCookie(response,user,token);
		}
		return user;
	}
	
//	public MiaoshaUser getByToken(HttpServletResponse response, String token) {
//		if (StringUtils.isEmpty(token)) {
//			return null;
//		}
//		MiaoshaUser user = redisService.get(MiaoshaUserKey.token, token, MiaoshaUser.class); // 延长有效期
//		if (user != null) {
//			addCookie(response, token, user);
//		}
//		return user;
//	}

//	public boolean login(HttpServletResponse response, LoginVo loginVo) {
//	  if(loginVo == null) { throw new GlobalException(CodeMsg.SERVER_ERROR); }
//	  String mobile = loginVo.getMobile(); String formPass = loginVo.getPassword();
//	  //判断手机号是否存在 MiaoshaUser user = getById(Long.parseLong(mobile)); if(user ==
//	  null) { throw new GlobalException(CodeMsg.MOBILE_NOT_EXIST); } //验证密码 String
//	  dbPass = user.getPassword(); String saltDB = user.getSalt(); String calcPass
//	  = MD5Util.formPassToDBPass(formPass, saltDB); if(!calcPass.equals(dbPass)) {
//	  throw new GlobalException(CodeMsg.PASSWORD_ERROR); } //生成cookie String token
//	  = UUIDUtil.uuid(); addCookie(response, token, user); return true; }
//
	private void addCookie(HttpServletResponse response, MiaoshaUser user,String token) {
		//生成cookie
		redisService.set(MiaoshaUserKey.token, token, user);
		Cookie cookie = new Cookie(COOKI_NAME_TOKEN, token);
		cookie.setMaxAge(MiaoshaUserKey.token.expireSeconds());
		cookie.setPath("/");
		response.addCookie(cookie);
	}

}
